/**
 * 
 */
package org.xelnaga.defiler.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.xelnaga.defiler.dao.support.SearchParameters;
import org.xelnaga.defiler.dao.support.SearchTemplate;
import org.xelnaga.defiler.domain.ContentletModel;
import org.xelnaga.defiler.domain.ContentletWrapper;
import org.xelnaga.defiler.domain.FolderModel;
import org.xelnaga.defiler.service.ContentletService;
import org.xelnaga.defiler.service.FieldService;
import org.xelnaga.defiler.service.FolderService;
import org.xelnaga.defiler.web.page.mapping.CacheableViewController;
import org.xelnaga.defiler.web.util.PagerDisplayImpl;

import edu.emory.mathcs.backport.java.util.Arrays;

/**
 * 处理产品列表页翻页等等。
 * @author qijb
 *
 */
@Controller("cacheableListPageController")
public class CacheableListPageController extends CacheableViewController {
	@Autowired
	private ContentletService contentletService;
	@Autowired
	private FolderService folderService;
	@Autowired
	private FieldService fieldService;
	
	@SuppressWarnings("unchecked")
	private static List<String> excludedProperties = Arrays.asList(new String[]{"inode", "start"});
	/**
	 * 
	 */
	public CacheableListPageController() {
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void populateModel(Model model, HttpServletRequest request) {
//		super.populateModel(model, request);
		SearchParameters parameters = new SearchParameters();
		
		String folderInode = request.getParameter("inode");
		FolderModel folder = null;
		if(StringUtils.isNotBlank(folderInode)) {
			folder = folderService.getByPrimaryKey(Long.valueOf(folderInode));
		}
		//Set the pageNumber of current search.
		String start = request.getParameter("page");
		if(StringUtils.isNotBlank(start) && StringUtils.isNumeric(start)) {
			parameters.setPageNumber(Integer.valueOf(start));
		}
		
		SearchTemplate searchTemplate = parameters.convertToSearchTemplate();
		@SuppressWarnings("unchecked")
		Map<String, Object> map = request.getParameterMap();
		for(Map.Entry<String, Object> entry : map.entrySet()) {
			if(!excludedProperties.contains(entry.getKey())) {
				searchTemplate.addParameter(entry.getKey(), entry.getValue());
			}
		}
		
		int total = contentletService.getCountByFolderWithPager(folder, searchTemplate);
		if(total > 0) {
			List<ContentletModel> list = contentletService.getListByFolderWithPager(folder, searchTemplate);
			
			List<ContentletWrapper> wrapperList = new ArrayList<ContentletWrapper>();
			for(ContentletModel content : list) {
				ContentletWrapper wrapper = new ContentletWrapper(fieldService.getByStructureInode(content.getStructureInode()), content);
				wrapperList.add(wrapper);
			}
			model.addAttribute("contentletPager", new PagerDisplayImpl<ContentletWrapper>(wrapperList, 
					parameters.getPageSize(), parameters.getPageNumber(), total));
		} else {
			model.addAttribute("contentletPager", new PagerDisplayImpl<ContentletWrapper>(new ArrayList<ContentletWrapper>(), 
					parameters.getPageSize(), parameters.getPageNumber(), total));
		}
	}

	public void setContentletService(ContentletService contentletService) {
		this.contentletService = contentletService;
	}

	public void setFolderService(FolderService folderService) {
		this.folderService = folderService;
	}

	public void setFieldService(FieldService fieldService) {
		this.fieldService = fieldService;
	}
	
}
